import { g6StorageValue } from 'glasssix-utils-ts';

const isAuth = (el: HTMLElement, binding: Record<string, any>): any => {
	const value = binding.value;
	if (!value) {
		return;
	}
	const { btn, page } = value;

	const flatMenus = Array.isArray(g6StorageValue.get('session', 'flatMenus'))
		? (g6StorageValue.get('session', 'flatMenus') as TsGlobalType.TsRoleMenu[])
		: [];
	// 当前菜单页面
	const curMenu = flatMenus.find((item: TsGlobalType.TsRoleMenu) => item.code === page);
	const pageBtnCodes = curMenu ? (curMenu as TsGlobalType.TsRoleMenu).btnCodes : [];

	if (pageBtnCodes.length === 0) {
		el.parentNode?.removeChild(el);
		return;
	}

	if (!pageBtnCodes.includes(btn)) {
		el.parentNode?.removeChild(el);
	}
};

export default {
	// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
	mounted(el: HTMLElement, binding: any) {
		isAuth(el, binding);
	}
};
